package com.itrencai.d16_socket.socket4;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

//使用线程池优化，实现通信
public class ServerDemo4 {
    public static void main(String[] args) {
        //使用静态变量记住一个线程池对象
        ExecutorService pool=new ThreadPoolExecutor(3,5,
                6, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());


        //1.注册端口
        try {
            System.out.println("==============服务端启动成功============");
            ServerSocket serverSocket=new ServerSocket(6666);
            //定义一个死循环由主线程负责不断的接收客户端的socket管道连接
            while (true) {
                //2.每接收到一个客户端的socket管道都交给一个独立的线程负责读取消息
                Socket socket=serverSocket.accept();
                System.out.println(socket.getRemoteSocketAddress()+"他来了，上线了");
                //任务对象负责读取数据
                Runnable target= new ServerReaderRunnable(socket);
                pool.execute(target);







            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

